//
//  KATTabBar.m
//  KATFramework
//
//  Created by Yi Yu on 2017/5/26.
//  Copyright © 2017年 KatApp. All rights reserved.
//

#import "KATTabBar.h"
#import "KATIntArray.h"
#import "KATButton.h"
#import "KATRouter.h"



NSString *const kTabBarClickingKey=@"tab_bar_clicking";



@interface KATTabBar ()

///按钮数组
@property(nonatomic,retain) KATArray<UIView *> *buttonArray;

///图片控件数组
@property(nonatomic,retain) KATArray<KATSprite *> *imageViewArray;

///标签数组
@property(nonatomic,retain) KATArray<UILabel *> *labelArray;

///徽标数组(圆点)
@property(nonatomic,retain) KATArray<KATShape *> *badgeArray;

///数字徽标数组
@property(nonatomic,retain) KATArray<KATLabel *> *numberBadgeArray;

///显示的徽标数组
@property(nonatomic,retain) KATIntArray *badgeStatusArray;

///显示的徽标数字数组
@property(nonatomic,retain) KATIntArray *badgeNumberArray;

///是否显示中
@property(nonatomic,assign) BOOL isShown;

///标签数
@property(nonatomic,assign) unsigned tabCount;

///顶线
@property(nonatomic,retain) UIView *topLine;

///中心按钮
@property(nonatomic,retain) KATSprite *centerButton;

///中心按钮顶线
@property(nonatomic,retain) KATShape *centerButtonLine;

///中心按钮增加的间距
@property(nonatomic,assign) double centerButtonMarginTop;

///中心按钮点击事件
@property(nonatomic,copy) void (^centerButtonAction)(void);

///中心按钮模式蒙版
@property(nonatomic,retain) UIView *maskView;

///是否为中心按钮模式
@property(nonatomic,assign) BOOL isCenterButtonMode;

@end



@implementation KATTabBar



//获取实例(纯文本风格)
+ (instancetype)tabBarWithUnselectedColor:(UIColor *)uColor selectedColor:(UIColor *)sColor text:(KATArray<NSString *> *)text andHosts:(KATArray<NSString *> *)hosts
{
    KATTabBar *tab=[self tabBarWithStyle:KATTabBarStyleLabel count:text.length unselectedColor:uColor selectedColor:sColor unselectedImages:nil selectedImages:nil text:text andHosts:hosts];
    
    tab.padding=0.0;
    tab.height=30.0+SCREEN_MARGIN_BOTTOM;
    tab.imageHeight=0.0;
    tab.labelHeight=12.0;
    
    [tab update];
    
    return tab;
}


//获取实例(纯图片风格)
+ (instancetype)tabBarWithUnselectedImages:(KATArray<UIImage *> *)uImages selectedImages:(KATArray<UIImage *> *)sImages andHosts:(KATArray<NSString *> *)hosts
{
    KATTabBar *tab=[self tabBarWithStyle:KATTabBarStyleImage count:uImages.length unselectedColor:nil selectedColor:nil unselectedImages:uImages selectedImages:sImages text:nil andHosts:hosts];
    
    tab.padding=0.0;
    tab.height=36.0+SCREEN_MARGIN_BOTTOM;
    tab.imageHeight=30.0;
    tab.labelHeight=0.0;
    
    [tab update];
    
    return tab;
}


//获取实例(纯图片风格，通过KATButton来构造)
+ (instancetype)tabBarWithButtonTypes:(NSArray<NSNumber *> *)types lineWidth:(float)lineWidth scale:(float)scale unselectedColor:(UIColor *)uColor selectedColor:(UIColor *)sColor andHosts:(KATArray<NSString *> *)hosts
{
    KATArray<UIImage *> *uImages=[KATArray arrayWithCapacity:(int)types.count];
    KATArray<UIImage *> *sImages=[KATArray arrayWithCapacity:(int)types.count];
    
    for(int i=0;i<types.count;i++)
    {
        KATButton *ub=[KATButton buttonWithFrame:RECT(0, 0, 30.0, 30.0) type:[types[i] intValue] color:uColor lineWidth:lineWidth scale:scale andOnClickAction:nil];
        [uImages put:[ub button2Image]];
        
        KATButton *sb=[KATButton buttonWithFrame:RECT(0, 0, 30.0, 30.0) type:[types[i] intValue] color:sColor lineWidth:lineWidth scale:scale andOnClickAction:nil];
        [sImages put:[sb button2Image]];
    }
    
    KATTabBar *tab=[self tabBarWithStyle:KATTabBarStyleImage count:uImages.length unselectedColor:uColor selectedColor:sColor unselectedImages:uImages selectedImages:sImages text:nil andHosts:hosts];
    
    tab.padding=0.0;
    tab.height=36.0+SCREEN_MARGIN_BOTTOM;
    tab.imageHeight=30.0;
    tab.labelHeight=0.0;
    
    [tab update];
    
    return tab;
}


//获取实例(通过KATButton来构造)
+ (instancetype)tabBarWithButtonTypes:(NSArray<NSNumber *> *)types lineWidth:(float)lineWidth scale:(float)scale unselectedColor:(UIColor *)uColor selectedColor:(UIColor *)sColor text:(KATArray<NSString *> *)text andHosts:(KATArray<NSString *> *)hosts
{
    KATArray<UIImage *> *uImages=[KATArray arrayWithCapacity:(int)types.count];
    KATArray<UIImage *> *sImages=[KATArray arrayWithCapacity:(int)types.count];
    
    for(int i=0;i<types.count;i++)
    {
        KATButton *ub=[KATButton buttonWithFrame:RECT(0, 0, 30.0, 30.0) type:[types[i] intValue] color:uColor lineWidth:lineWidth scale:scale andOnClickAction:nil];
        [uImages put:[ub button2Image]];
        
        KATButton *sb=[KATButton buttonWithFrame:RECT(0, 0, 30.0, 30.0) type:[types[i] intValue] color:sColor lineWidth:lineWidth scale:scale andOnClickAction:nil];
        [sImages put:[sb button2Image]];
    }
    
    return [self tabBarWithStyle:KATTabBarStyleImageLabel count:uImages.length unselectedColor:uColor selectedColor:sColor unselectedImages:uImages selectedImages:sImages text:text andHosts:hosts];
}


//获取实例(全参数)
+ (instancetype)tabBarWithStyle:(KATTabBarStyle)style count:(unsigned)count unselectedColor:(UIColor *)uColor selectedColor:(UIColor *)sColor unselectedImages:(KATArray<UIImage *> *)uImages selectedImages:(KATArray<UIImage *> *)sImages text:(KATArray<NSString *> *)text andHosts:(KATArray<NSString *> *)hosts
{
    KATTabBar *tab=[[[self alloc] init] autorelease];
        
    if(count>TAB_BAR_COUNT_MAX)
    {
        count=TAB_BAR_COUNT_MAX;
    }
    
    tab.bounds=RECT(0, 0, MAX(SCREEN_WIDTH, SCREEN_HEIGHT), 0);
    tab.tabCount=count;
    tab.style=style;
    tab.unselectedColor=uColor;
    tab.selectedColor=sColor;
    tab.unselectedImageArray=uImages;
    tab.selectedImageArray=sImages;
    tab.textArray=text;
    tab.hostArray=hosts;
    tab.padding=3.0;//会自动计算
    tab.height=49.0+SCREEN_MARGIN_BOTTOM;
    tab.imageHeight=30.0;
    tab.labelHeight=10.0;
    tab.transitionStyle=KATRouterTransitionStyleNone;
    tab.transitionDuration=0.0;
    tab.tapAnimation=KATTabBarAnimationNone;
    tab.isCenterButtonMode=NO;
    tab.hasAnimation=YES;
    tab.animationDuration=0.12;
    tab.buttonTapedAction=nil;
    
    tab.backgroundColor=KCOLOR_CLEAR;
    
    //模糊层
    tab.blurView=[[[UIVisualEffectView alloc] init] autorelease];
    tab.blurView.effect=[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    [tab addSubview:tab.blurView];
    
    
    //顶线(直线)
    tab.topLine=[[[UIView alloc] init] autorelease];
    tab.topLine.backgroundColor=KCOLOR_LINE;
    tab.topLine.userInteractionEnabled=NO;
    [tab.blurView.contentView addSubview:tab.topLine];
    
    
    //中心按钮顶线
    tab.centerButtonLine=nil;
    
    
    tab.buttonArray=[KATArray arrayWithCapacity:tab.tabCount];
    tab.imageViewArray=[KATArray arrayWithCapacity:tab.tabCount];
    tab.labelArray=[KATArray arrayWithCapacity:tab.tabCount];
    tab.badgeArray=[KATArray arrayWithCapacity:tab.tabCount];
    tab.badgeStatusArray=[KATIntArray intArrayWithCapacity:tab.tabCount];
    tab.numberBadgeArray=[KATArray arrayWithCapacity:tab.tabCount];
    tab.badgeNumberArray=[KATIntArray intArrayWithCapacity:tab.tabCount];
    
    UITapGestureRecognizer *tap=nil;
    
    //数字徽标样式
    tab.numberBadgeStyle=[KATTextStyle textStyle];
    tab.numberBadgeStyle.textColor=KCOLOR_WHITE;
    tab.numberBadgeStyle.textBgColor=KCOLOR_CLEAR;
    tab.numberBadgeStyle.bgColor=KCOLOR_RED;
    tab.numberBadgeStyle.textAlignment=NSTextAlignmentCenter;
    tab.numberBadgeStyle.textFont=[UIFont systemFontOfSize:11.0];
    tab.numberBadgeStyle.marginTop=3.0;
    tab.numberBadgeStyle.marginBottom=3.0;
    tab.numberBadgeStyle.marginLeft=3.0;
    tab.numberBadgeStyle.marginRight=3.0;
    
    for(int i=0;i<tab.tabCount;i++)
    {
        //按钮
        UIView *button=[[[UIView alloc] init] autorelease];
        button.backgroundColor=KCOLOR_CLEAR;
        button.userInteractionEnabled=YES;
        button.tag=i;
        
        tap=[[[UITapGestureRecognizer alloc] initWithTarget:tab action:@selector(actionTap:)] autorelease];
        [button addGestureRecognizer:tap];
        
        [tab addSubview:button];
        [tab.buttonArray put:button];
        
        //图片
        KATSprite *imageV=[KATSprite sprite];
        imageV.backgroundColor=KCOLOR_CLEAR;
        imageV.spriteDelegate=tab;
        [button addSubview:imageV];
        [tab.imageViewArray put:imageV];
        
        //标签
        UILabel *label=[[[UILabel alloc] init] autorelease];
        label.backgroundColor=KCOLOR_CLEAR;
        label.textAlignment=NSTextAlignmentCenter;
        [button addSubview:label];
        [tab.labelArray put:label];
        
        //徽标
        KATShape *badge=[KATShapeUtil roundShapeWithRadius:3.5];
        badge.fillColor=KCOLOR_RED.CGColor;
        badge.lineWidth=0;
        [button.layer addSublayer:badge];
        [tab.badgeArray put:badge];
        
        //徽标状态
        [tab.badgeStatusArray put:0];
        
        //数字徽标
        KATLabel *numberBadge=[KATLabel labelWithFrame:RECT(0, 0, 0, 0) andStyle:tab.numberBadgeStyle];
        [button addSubview:numberBadge];
        [tab.numberBadgeArray put:numberBadge];
        
        //徽标数字
        [tab.badgeNumberArray put:0];
    }
    
    tab.badgeColor=KCOLOR_RED;
    
    tab.isShown=NO;
    
    tab.bounds=BOUNDS(MAX(SCREEN_WIDTH, SCREEN_HEIGHT), tab.height);
    tab.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT+tab.bounds.size.height*0.5);
    
//    [tab selectWithIndex:0];
    
    return tab;
}


//更新内容
- (void)update
{    
    //中心按钮索引
    int centerIndex=_tabCount+1;
    
    if(_centerButton && _tabCount>0 && _tabCount%2==0 && _style==KATTabBarStyleImageLabel)//中心模式
    {
        _isCenterButtonMode=YES;
        centerIndex=_tabCount/2;
    }
    
    //计算高度
    float buttonWidth=0;
    
    //中心按钮模式上间距
    float marginTop=0;
    
    if(_style==KATTabBarStyleImage)
    {
        _padding=(_height-SCREEN_MARGIN_BOTTOM-_imageHeight)/2.0;
        _labelHeight=0;
    }
    else if(_style==KATTabBarStyleLabel)
    {
        _padding=0;
        _imageHeight=0;
    }
    else
    {
        _padding=(_height-SCREEN_MARGIN_BOTTOM-_imageHeight-_labelHeight)/3.0;
        
        if(_isCenterButtonMode)//中心模式
        {
            marginTop=(_imageHeight*0.5+_padding)*2;
        }
    }
    
    _centerButtonMarginTop=marginTop;
    
    if(_tabCount>0)
    {
        if(DEVICE_IPAD || DEVICE_IPAD_PRO)//iPad模式
        {
            //个数
            int count=_tabCount+(_isCenterButtonMode?1:0);
            
            if(count<6)//6个以内做两边填充处理
            {
                buttonWidth=SCREEN_WIDTH*1.0/(count+2);
            }
            else//6个以上不处理
            {
                buttonWidth=SCREEN_WIDTH*1.0/count;
            }
        }
        else//iPhone模式，均分
        {
            buttonWidth=SCREEN_WIDTH*1.0/(_tabCount+(_isCenterButtonMode?1:0));
        }
    }
    
    //尺寸
    self.bounds=BOUNDS(self.bounds.size.width, _height+marginTop);
//    self.frame=RECT(-(self.bounds.size.width-SCREEN_WIDTH)*0.5, _isShown?SCREEN_HEIGHT-self.bounds.size.height:SCREEN_HEIGHT, self.bounds.size.width, self.bounds.size.height);
    
    //左间距
    float marginLeft=(self.bounds.size.width-SCREEN_WIDTH)*0.5;
    
    //模糊效果
    _blurView.frame=self.bounds;
    
    //布局
    for(int i=0;i<_tabCount;i++)
    {
        UIView *button=_buttonArray[i];
        
        if(DEVICE_IPAD || DEVICE_IPAD_PRO)//iPad模式
        {
            //个数
            int count=_tabCount+(_isCenterButtonMode?1:0);
            
            if(count<6)//6个以内做两边填充处理
            {
                if(_isCenterButtonMode && i>=centerIndex)
                {
                    button.frame=RECT(marginLeft+self.layer.borderWidth+buttonWidth*(i+2), marginTop, buttonWidth, _height-SCREEN_MARGIN_BOTTOM);
                }
                else
                {
                    button.frame=RECT(marginLeft+self.layer.borderWidth+buttonWidth*(i+1), marginTop, buttonWidth, _height-SCREEN_MARGIN_BOTTOM);
                }
            }
            else//6个以上不处理
            {
                if(_isCenterButtonMode && i>=centerIndex)
                {
                    button.frame=RECT(marginLeft+self.layer.borderWidth+buttonWidth*(i+1), marginTop, buttonWidth, _height-SCREEN_MARGIN_BOTTOM);
                }
                else
                {
                    button.frame=RECT(marginLeft+self.layer.borderWidth+buttonWidth*i, marginTop, buttonWidth, _height-SCREEN_MARGIN_BOTTOM);
                }
            }
        }
        else//iPhone模式
        {
            if(_isCenterButtonMode && i>=centerIndex)
            {
                button.frame=RECT(marginLeft+self.layer.borderWidth+buttonWidth*(i+1), marginTop, buttonWidth, _height-SCREEN_MARGIN_BOTTOM);
            }
            else
            {
                button.frame=RECT(marginLeft+self.layer.borderWidth+buttonWidth*i, marginTop, buttonWidth, _height-SCREEN_MARGIN_BOTTOM);
            }
        }
        
        KATSprite *imageV=_imageViewArray[i];
        imageV.frame=RECT((buttonWidth-_imageHeight)*0.5, _padding, _imageHeight, _imageHeight);
        imageV.contents=_unselectedImageArray[i];
        imageV.hidden=NO;
        
        UILabel *label=_labelArray[i];
        label.frame=RECT(_padding*0.5, _padding+_imageHeight, buttonWidth-_padding*1, (_style==KATTabBarStyleLabel)?_height:_labelHeight+_padding*2);
        label.text=_textArray[i];
        label.textColor=_unselectedColor;
        label.font=[UIFont systemFontOfSize:_labelHeight];
        label.textAlignment=NSTextAlignmentCenter;
        label.hidden=NO;
        
        KATShape *badge=_badgeArray[i];
        badge.position=POINT(imageV.frame.origin.x+imageV.bounds.size.width, badge.bounds.size.height);
        badge.hidden=YES;
        
        if([_badgeStatusArray get:i]>0)
        {
            badge.fillColor=_badgeColor.CGColor;
            badge.hidden=NO;
        }
        
        KATLabel *numberBadge=_numberBadgeArray[i];
        numberBadge.hidden=YES;
        
        if([_badgeNumberArray get:i]>0)
        {
            int num=[_badgeNumberArray get:i];
            numberBadge.frame=RECT(0, 0, button.bounds.size.width/2.0, button.bounds.size.height/2.0);
            numberBadge.hidden=NO;
            
            if(num<100)//小于99
            {
                numberBadge.text=[NSString stringWithFormat:@"%i",num];
            }
            else//大于99
            {
                numberBadge.text=@"99+";
            }
            
            [numberBadge resizeFrame];
            
            //非圆形
            if(numberBadge.frame.size.width<numberBadge.frame.size.height)
            {
                numberBadge.frame=RECT(0, 0, numberBadge.frame.size.height, numberBadge.frame.size.height);
            }
            
            numberBadge.layer.cornerRadius=numberBadge.frame.size.height/2.0;
            numberBadge.layer.masksToBounds=YES;
            numberBadge.center=badge.position;
        }
        
        if(i==_selectedIndex)//选中
        {
            imageV.contents=_selectedImageArray[i];
            label.textColor=_selectedColor;
        }
        
        if(_style==KATTabBarStyleLabel)
        {
            imageV.hidden=YES;
        }
        
        if(_style==KATTabBarStyleImage)
        {
            label.hidden=YES;
        }
    }
    
    //中心按钮模式
    if(_isCenterButtonMode)
    {
        //中心线位置
        _centerButtonLine.position=POINT(_blurView.bounds.size.width*0.5, marginTop-_centerButtonLine.bounds.size.height*0.5);
    }
    else
    {
        //顶线
        _topLine.frame=RECT(0, marginTop, _blurView.bounds.size.width, 0.5);
    }
}


//添加中心按钮(这个方法的代码特别是在设置高度时有不少重复，有点锉，为了效率，暂时先这样)
- (void)addCenterButton:(KATSprite *)button withOnClickAction:(void (^)(void))action
{
    if(self.tabCount>0 && self.tabCount%2==0 && _style==KATTabBarStyleImageLabel)
    {
        if(button && button!=_centerButton)
        {
            _isCenterButtonMode=YES;
            
            //计算高度
            float marginTop=(_imageHeight*0.5+_padding)*2;
            
            //先做删除操作
            [self removeCenterButton];
            
            self.centerButton=button;
            self.centerButtonAction=action;
            
            
            //尺寸
            self.bounds=BOUNDS(self.bounds.size.width, _height+marginTop);
            self.frame=RECT(-(self.bounds.size.width-SCREEN_WIDTH)*0.5, _isShown?SCREEN_HEIGHT-self.bounds.size.height+0.5:SCREEN_HEIGHT+0.5, self.bounds.size.width, self.bounds.size.height);
            
            //模糊效果
            _blurView.frame=self.bounds;
            
            
            //tab蒙版
            self.maskView=[[[UIView alloc] init] autorelease];
            _maskView.bounds=RECT(0, 0, MAX(SCREEN_WIDTH, SCREEN_HEIGHT), _blurView.bounds.size.height);
            _maskView.center=POINT(_blurView.bounds.size.width*0.5, _blurView.bounds.size.height*0.5);
            
            //矩形
            KATShape *rect=[KATShapeUtil rectShapeWithWidth:_maskView.bounds.size.width andHeight:_height];
            rect.fillColor=KCOLOR_RED.CGColor;
            rect.lineWidth=0;
            rect.position=POINT(_maskView.bounds.size.width*0.5, marginTop+_height*0.5);
            [_maskView.layer addSublayer:rect];
            
            //圆形
            KATShape *round=[KATShapeUtil roundShapeWithRadius:(_imageHeight*0.5+_padding)*2.0];
            round.fillColor=KCOLOR_RED.CGColor;
            round.lineWidth=0;
            round.position=POINT(_maskView.bounds.size.width*0.5, marginTop+_imageHeight*0.5+_padding);
            [_maskView.layer addSublayer:round];
            
            //设置蒙版
            _blurView.maskView=_maskView;
            
            
            //顶线
            [_topLine removeFromSuperview];
            
            //中心按钮顶线
            self.centerButtonLine=[KATShape shape];
            _centerButtonLine.bounds=CGRectMake(0, 0, MAX(SCREEN_WIDTH, SCREEN_HEIGHT), (_imageHeight*0.5+_padding));
            _centerButtonLine.fillColor=[UIColor clearColor].CGColor;
            _centerButtonLine.strokeColor=KCOLOR_LINE.CGColor;
            _centerButtonLine.lineJoin=kCALineJoinRound;
            _centerButtonLine.lineCap=kCALineCapRound;
            _centerButtonLine.lineWidth=0.5f;
            
            UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
            
            [path moveToPoint:CGPointMake(0, _centerButtonLine.bounds.size.height)];
            [path addLineToPoint:CGPointMake(_centerButtonLine.bounds.size.width*0.5-sin(M_PI/3.0)*((_imageHeight*0.5+_padding)*2.0), _centerButtonLine.bounds.size.height)];
            [path addArcWithCenter:POINT(_centerButtonLine.bounds.size.width*0.5, (_imageHeight*0.5+_padding)*2.0) radius:(_imageHeight*0.5+_padding)*2.0 startAngle:M_PI+M_PI/6.0 endAngle:M_PI+M_PI/6.0*5.0 clockwise:YES];
            [path addLineToPoint:POINT(_centerButtonLine.bounds.size.width, _centerButtonLine.bounds.size.height)];
            
            _centerButtonLine.path=path.CGPath;
            
            [_blurView.layer addSublayer:_centerButtonLine];
            
            
            //按钮
            _centerButton.bounds=RECT(0, 0, (_imageHeight*0.5+_padding)*2.0*2.0-_padding*2.0, (_imageHeight*0.5+_padding)*2.0*2.0-_padding*2.0);
            _centerButton.center=POINT(_blurView.bounds.size.width*0.5, marginTop+(_imageHeight*0.5+_padding));
            _centerButton.hidden=NO;
            
            //按钮蒙版
            KATShape *buttonMask=[KATShapeUtil roundShapeWithRadius:_imageHeight];
            buttonMask.fillColor=KCOLOR_RED.CGColor;
            buttonMask.lineWidth=0;
            buttonMask.position=POINT(_centerButton.bounds.size.width*0.5, _centerButton.bounds.size.height*0.5);
            
            _centerButton.layer.mask=buttonMask;
            
            //添加点击事件
            if(_centerButtonAction)
            {
                UITapGestureRecognizer *tap=[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(centerButtonClicked)] autorelease];
                [_centerButton addGestureRecognizer:tap];
            }
            
            //添加到tab栏
            [_blurView.contentView addSubview:button];
            
            //刷新
            [self update];
            
            [_centerButton play];
        }
    }
}


//删除中心按钮
- (void)removeCenterButton
{
    _isCenterButtonMode=NO;
    
    //中心按钮线
    if(_centerButtonLine)
    {
        [_centerButtonLine removeFromSuperlayer];
        self.centerButtonLine=nil;
    }
    
    //顶线
    [_blurView.contentView addSubview:_topLine];
    
    //去掉蒙版
    _blurView.maskView=nil;
    self.maskView=nil;
    
    //中心按钮
    if(_centerButton)
    {
        [_centerButton removeFromSuperview];
        
        self.centerButton=nil;
        
        [self update];
    }
    
    if(_centerButtonAction)
    {
        Block_release(_centerButtonAction);
        
        _centerButtonAction=nil;
    }
}



//中心按钮点击事件
- (void)centerButtonClicked
{
    if(_centerButtonAction)
    {
        _centerButtonAction();
    }
}


//设置选中的索引
- (void)setSelectedIndex:(unsigned int)selectedIndex
{
    _selectedIndex=selectedIndex;
    
    [self removeBadgeWithIndex:_selectedIndex];
}


//选中索引
- (void)selectWithIndex:(unsigned)index
{
    if(index>=_tabCount)
    {
        return;
    }
    
    if([KATRouter getTabBar]==self && _hostArray[index])
    {
        KATHashMap *map=[KATURIParser parseURI:_hostArray[index]];
        
        if(!map[kURIKeyQuery])
        {
            map[kURIKeyQuery]=[KATHashMap hashMapWithCapacity:10 andMaxUsage:7];
        }
        
        map[kURIKeyQuery][kTabBarClickingKey]=@"1";
        
        [KATRouter routeWithURI:[KATURIParser URIWithMap:map]];
    }
    
    self.selectedIndex=index;
    
    [self update];
}


//选中host
- (void)selectWithHost:(NSString *)host
{
    if(!host)
    {
        return;
    }
    
    for(int i=0;i<_hostArray.length;i++)
    {
        KATHashMap *hostMap=[KATURIParser parseURI:_hostArray[i]];
        
        if([host isEqualToString:hostMap[kURIKeyHost]])
        {
            [self selectWithIndex:i];
            
            break;
        }
    }
}


//通过index添加标签栏的圆点徽标
- (void)addBadgeWithIndex:(unsigned)index
{
    if(index>=_tabCount)
    {
        return;
    }
    
    [_badgeStatusArray set:1 withIndex:index];
    
    [self update];
}


//通过host添加标签栏的圆点徽标
- (void)addBadgeWithHost:(NSString *)host
{
    if(!host)
    {
        return;
    }
    
    for(int i=0;i<_hostArray.length;i++)
    {
        KATHashMap *URI=[KATURIParser parseURI:_hostArray[i]];
        
        if([host isEqualToString:URI[kURIKeyHost]])
        {
            [self addBadgeWithIndex:i];
            
            break;
        }
    }
}


//通过index删除标签栏的圆点徽标
- (void)removeBadgeWithIndex:(unsigned)index
{
    if(index>=_tabCount)
    {
        return;
    }
    
    if([_badgeStatusArray get:index]!=0)
    {
        [_badgeStatusArray set:0 withIndex:index];
        
        [self update];
    }
}


//通过host删除标签栏的圆点徽标
- (void)removeBadgeWithHost:(NSString *)host
{
    if(!host)
    {
        return;
    }
    
    for(int i=0;i<_hostArray.length;i++)
    {
        KATHashMap *URI=[KATURIParser parseURI:_hostArray[i]];
        
        if([host isEqualToString:URI[kURIKeyHost]])
        {
            [self removeBadgeWithIndex:i];
            
            break;
        }
    }
}


//通过index获取标签栏的徽标数字
- (int)badgeNumberWithIndex:(unsigned)index
{
    if(index>=_tabCount)
    {
        return 0;
    }
    
    return [_badgeNumberArray get:index];
}


//通过host获取标签栏的徽标数字
- (int)badgeNumberWithHost:(NSString *)host
{
    if(!host)
    {
        return 0;
    }
    
    for(int i=0;i<_hostArray.length;i++)
    {
        KATHashMap *URI=[KATURIParser parseURI:_hostArray[i]];
        
        if([host isEqualToString:URI[kURIKeyHost]])
        {
            return [self badgeNumberWithIndex:i];
        }
    }
    
    return 0;
}


//通过index设置标签栏的数字徽标
- (void)setBadgeNumber:(int)number withIndex:(unsigned)index
{
    if(index>=_tabCount)
    {
        return;
    }
    
    if(number<0)
    {
        number=0;
    }
    
    [_badgeNumberArray set:number withIndex:index];
    
    [self update];
}


//通过host设置标签栏的数字徽标
- (void)setBadgeNumber:(int)number withHost:(NSString *)host
{
    if(!host)
    {
        return;
    }
    
    for(int i=0;i<_hostArray.length;i++)
    {
        KATHashMap *URI=[KATURIParser parseURI:_hostArray[i]];
        
        if([host isEqualToString:URI[kURIKeyHost]])
        {
            [self setBadgeNumber:number withIndex:i];
            
            break;
        }
    }
}


//清除所有徽标
- (void)clearBadges
{
    for(int i=0;i<_badgeStatusArray.length;i++)
    {
        [_badgeStatusArray set:0 withIndex:i];
    }
    
    for(int i=0;i<_badgeNumberArray.length;i++)
    {
        [_badgeNumberArray set:0 withIndex:i];
    }
    
    [self update];
}


//更改图片
- (void)changeWithUnselectedImage:(UIImage *)uImage selectedImage:(UIImage *)sImage forHost:(NSString *)host
{
    if(!host)
    {
        return;
    }
    
    for(int i=0;i<_hostArray.length;i++)
    {
        KATHashMap *URI=[KATURIParser parseURI:_hostArray[i]];
        
        if([host isEqualToString:URI[kURIKeyHost]])
        {
            [_unselectedImageArray set:uImage withIndex:i];
            [_selectedImageArray set:sImage withIndex:i];
            
            [self update];
            
            break;
        }
    }
}


//更改图片
- (void)changeWithUnselectedImage:(UIImage *)uImage selectedImage:(UIImage *)sImage forIndex:(unsigned)index
{
    if(index>=_tabCount)
    {
        return;
    }
    
    [_unselectedImageArray set:uImage withIndex:index];
    [_selectedImageArray set:sImage withIndex:index];
    
    [self update];
}


//更改文本
- (void)changeWithText:(NSString *)text forHost:(NSString *)host
{
    if(!host)
    {
        return;
    }
    
    for(int i=0;i<_hostArray.length;i++)
    {
        KATHashMap *URI=[KATURIParser parseURI:_hostArray[i]];
        
        if([host isEqualToString:URI[kURIKeyHost]])
        {
            [_textArray set:text withIndex:i];
            
            [self update];
            
            break;
        }
    }
}


//更改文本
- (void)changeWithText:(NSString *)text forIndex:(unsigned)index
{
    if(index>=_tabCount)
    {
        return;
    }
    
    [_textArray set:text withIndex:index];
    
    [self update];
}


//获取选中的host
- (NSString *)selectedHost
{
    KATHashMap *URI=[KATURIParser parseURI:[self selectedURI]];
    
    return URI[kURIKeyHost];
}


//获取选中的URI
- (NSString *)selectedURI
{
    return _hostArray[_selectedIndex];
}


//显示
- (void)show
{
    [self update];
    
    if(_isCenterButtonMode)
    {
        [_centerButton play];
    }
    
    if(!_isShown)
    {
        _isShown=YES;
        
        self.hidden=NO;
        
        if(_hasAnimation)
        {
            [UIView animateWithDuration:_animationDuration animations:^
            {
                self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT-self.bounds.size.height*0.5);
            }];
        }
        else
        {
            self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT-self.bounds.size.height*0.5);
        }
    }
    else
    {        
        self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT-self.bounds.size.height*0.5);
    }
   
}


//隐藏
- (void)hide
{
    [self update];
    
    if(_isCenterButtonMode)
    {
        [_centerButton stop];
    }
    
    if(_isShown)
    {
        _isShown=NO;
        
        if(_hasAnimation)
        {
            [UIView animateWithDuration:_animationDuration animations:^
            {
                self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT+self.bounds.size.height*0.5);
            }
            completion:^(BOOL finished)
            {
                self.hidden=YES;
            }];
        }
        else
        {
            self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT+self.bounds.size.height*0.5);
            
            self.hidden=YES;
        }
    }
    else
    {
        self.center=POINT(SCREEN_WIDTH*0.5, SCREEN_HEIGHT+self.bounds.size.height*0.5);
        
        self.hidden=YES;
    }
}


//判断host是否与tab栏关联
- (BOOL)isHostInTab:(NSString *)host
{
    if(host)
    {
        for(int i=0;i<_hostArray.length;i++)
        {
            KATHashMap *hostMap=[KATURIParser parseURI:_hostArray[i]];
            
            if([host isEqualToString:hostMap[kURIKeyHost]])
            {
                return YES;
            }
        }
        
    }
    
    return NO;
}


//判断VC是否与tab栏关联
- (BOOL)isInstanceInTab:(UIViewController *)instance
{
    if(instance)
    {
        NSString *host=objc_getAssociatedObject(instance, kRouterKeyHost);
        
        if(host)
        {
            return [self isHostInTab:host];
        }
    }
    
    return NO;
}


//点击事件
- (void)actionTap:(UITapGestureRecognizer *)tap
{
    [self selectWithIndex:(unsigned)tap.view.tag];
    
    //回调
    if(_buttonTapedAction)
    {
        _buttonTapedAction([self selectedHost]);
    }
    
    //点击动画
    KATSprite *tapedImage=_imageViewArray[tap.view.tag];
    
    if(tapedImage)
    {
        switch(_tapAnimation)
        {
            case KATTabBarAnimationNone:
                
                break;
                
                
            case KATTabBarAnimationShake:
                
                [tapedImage clearAnimations];
                [tapedImage restoreSprite];
                [tapedImage shakeWithDegree:-1 duration:-1 repeat:3];
                
                break;
                
                
            case KATTabBarAnimationBlink:
                
                [tapedImage clearAnimations];
                [tapedImage restoreSprite];
                [tapedImage blinkWithAlpha:-1 duration:-1 repeat:1];
                
                break;
                
                
            case KATTabBarAnimationHeartBeat:
                
                [tapedImage clearAnimations];
                [tapedImage restoreSprite];
                [tapedImage heartbeatWithScale:-1 duration:0.16 repeat:1];
                
                break;
                
                
            case KATTabBarAnimationZoom:
                
                [tapedImage clearAnimations];
                [tapedImage restoreSprite];
                tapedImage.alpha=0.0;
                [tapedImage zoomInWithScale:1.38 duration:0.5];
                
                break;
                
                
            case KATTabBarAnimationRotate:
                
                [tapedImage clearAnimations];
                [tapedImage restoreSprite];
                [tapedImage rotateWithDuration:0.36 repeat:1.0 clockwise:NO];
                
                break;
                
                
            case KATTabBarAnimationRotateClockwise:
                
                [tapedImage clearAnimations];
                [tapedImage restoreSprite];
                [tapedImage rotateWithDuration:0.36 repeat:1.0 clockwise:YES];
                
                break;
                
                
            case KATTabBarAnimationFlipHorizontal:
                
                [tapedImage clearAnimations];
                [tapedImage restoreSprite];
                [tapedImage flipWithDuration:0.5 repeat:1.0 clockwise:YES vertical:NO];
                
                break;
                
                
            case KATTabBarAnimationFlipVertical:
                
                [tapedImage clearAnimations];
                [tapedImage restoreSprite];
                [tapedImage flipWithDuration:0.5 repeat:1.0 clockwise:YES vertical:YES];
                
                break;
        }
    }
}


//子控件超出部分的事件响应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view=[super hitTest:point withEvent:event];
    
    if(_isCenterButtonMode)//中心按钮模式
    {
        //点击按钮或者图片
        if(view && ([_buttonArray hasMember:view] || [_imageViewArray hasMember:(KATSprite *)view]))
        {
            return view;
        }
        
        if(view && view==_centerButton)
        {
            //蒙版圆
            KATShape *shape=(KATShape *)_centerButton.layer.mask;
            
            //转换坐标系
            CGPoint newPoint=[shape convertPoint:point fromLayer:self.layer];
            
            if([shape containsPoint:newPoint])
            {
                return _centerButton;
            }
        }
        
        return nil;
    }
    
    return view;
}


//精灵动画结束
- (void)sprite:(KATSprite *)sprite didStopAnimation:(NSString *)animation finished:(BOOL)flag
{
    [self update];
}


//释放内存
- (void)dealloc
{
    if(_centerButtonAction)
    {
        Block_release(_centerButtonAction);
        _centerButtonAction=nil;
    }
    
    if(_buttonTapedAction)
    {
        Block_release(_buttonTapedAction);
        _buttonTapedAction=nil;
    }
    
    [_unselectedColor release];
    [_selectedColor release];
    [_unselectedImageArray release];
    [_selectedImageArray release];
    [_textArray release];
    [_hostArray release];
    [_buttonArray release];
    [_imageViewArray release];
    [_labelArray release];
    [_badgeArray release];
    [_badgeStatusArray release];
    [_numberBadgeArray release];
    [_badgeNumberArray release];
    [_blurView release];
    [_topLine release];
    [_centerButton release];
    [_centerButtonLine release];
    [_maskView release];
    [_badgeColor release];
    [_numberBadgeStyle release];
    
    [super dealloc];
}


@end
